package com.gd.coreservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gd.coreservice.entity.Orders;
import com.gd.coreservice.entity.vo.OrdersVo;
import com.gd.coreservice.mapper.OrdersMapper;
import com.gd.coreservice.service.OrdersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.NumberUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2024-03-23
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Override
    public List<Orders> findAll() {
        return baseMapper.selectList(null);
    }

    @Override
    public Orders getOneOrder(String oid) {
        return baseMapper.selectById(oid);
    }

    @Override
    public List<OrdersVo> findOneBUY(String sellerUid, String oid) {
        return baseMapper.findOneBuy(sellerUid,oid);
    }

    @Override
    public boolean insertOrder(String buySid, String sellSid, String gid, String oprice) {

        Orders orders = new Orders();
        BigDecimal bigDecimal = new BigDecimal(oprice);
        orders.setBuyerId(buySid).setSellerId(sellSid).setGid(gid).setPrice(bigDecimal);
        try{
            int insert = baseMapper.insert(orders);
            if (1==insert) return true;
            else return false;
        }
        catch (Exception e)
        {
            return false;
        }

    }

    @Override
    public Integer deleteOrder(String oid) {
        return baseMapper.deleteById(oid);
    }

    @Override
    public Integer deleteOrderBatch(List<String> oids) {
        return baseMapper.deleteBatchIds(oids);
    }

    @Override
    public Map<String,Object> getOrderPage(Integer pageNum, Integer pageSize, String oid) {
        Map<String,Object> result = new HashMap<>();
        Page<Orders> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("oid",oid);
        IPage<Orders> ordersIPage = baseMapper.selectPage(page, queryWrapper);
        result.put("List",ordersIPage.getRecords());
        result.put("total",ordersIPage.getTotal());
        return result;
    }

    @Override
    public int getAllTotal() {
        return baseMapper.selectCount(null);
    }

    @Override
    public String getPriceTotal() {
        return baseMapper.getPriceTotal();
    }
}
